home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE08 / SERVER / ALNEW.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1996-02-11  |  3.8 KB  |  153 lines

  1. unit ALNew;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Grids;
  8.  
  9. type
  10.   TNewAliasForm = class(TForm)
  11.     Alias: TEdit;
  12.     Label1: TLabel;
  13.     Driver: TComboBox;
  14.     Label2: TLabel;
  15.     OkBtn: TButton;
  16.     CancelBtn: TButton;
  17.     DriverParamsGrid: TStringGrid;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure OkBtnClick(Sender: TObject);
  20.     procedure DriverClick(Sender: TObject);
  21.   public
  22.     procedure SaveAlias;
  23.   end;
  24.  
  25. var
  26.   NewAliasForm: TNewAliasForm;
  27.  
  28. implementation
  29.  
  30. {$R *.DFM}
  31.  
  32. uses
  33.   DB, DbiProcs, DbiErrs;
  34.  
  35. procedure Split( Source: String; SplitChar: Char; var Left, Right: String);
  36. { Returns in <Left> the substring of <Source> that is to the left of
  37.   the first occurrence of <SplitChar> within <Source>.  Returns in
  38.   <Right> the substring of <Source> that is to the right of the first
  39.   occurrence of <SplitChar>. }
  40. var
  41.   I: Byte;
  42. begin
  43.   I := 1;
  44.   while (I <= Length(Source)) and (Source[I] <> SplitChar) Do Inc(I);
  45.   Left := Copy(Source, 1, I - 1);
  46.   Right := Copy(Source, I + 1, 255);
  47. end;
  48.  
  49. procedure TNewAliasForm.FormCreate(Sender: TObject);
  50. begin
  51.  
  52.   { Initialize the combo box dropdown list with available BDE drivers }
  53.   Session.GetDriverNames(Driver.Items);
  54.  
  55.   { Initialize the parameters grid }
  56.   with DriverParamsGrid do
  57.   begin
  58.     Cells[0, 0] := 'Parameter:';
  59.     Cells[1, 0] := 'Value:';
  60.     RowCount :=2;
  61.   end;
  62. end;
  63.  
  64. procedure TNewAliasForm.DriverClick(Sender: TObject);
  65. var
  66.   DriverParamsList: TStringList;
  67.   I: Integer;
  68.   ParamName: String;
  69.   ParamValue: String;
  70. begin
  71.   DriverParamsList := TStringList.Create;
  72.   try
  73.     with Driver do
  74.       Session.GetDriverParams(Items[ItemIndex], DriverParamsList);
  75.  
  76.     { Populate the driver parameters grid }
  77.     with DriverParamsGrid do
  78.     begin
  79.       RowCount := 1;
  80.       for I := 0 to DriverParamsList.Count - 1 do
  81.       begin
  82.         Split(DriverParamsList.Strings[I], '=', ParamName, ParamValue);
  83.         if ParamName <> 'PASSWORD' then
  84.         begin
  85.           Cells[0, I + 1] := ParamName;
  86.           Cells[1, I + 1] := ParamValue;
  87.           RowCount := RowCount + 1;
  88.         end;
  89.       end;
  90.       FixedRows := 1;
  91.  
  92.       SetFocus;
  93.     end;
  94.   finally
  95.     DriverParamsList.Free;
  96.   end;
  97. end;
  98.  
  99. procedure TNewAliasForm.OkBtnClick(Sender: TObject);
  100. begin
  101.   try
  102.     if Alias.Text = '' then
  103.       raise Exception.Create('Must supply an alias name');
  104.     if Driver.ItemIndex = -1 then
  105.       raise Exception.Create('Must supply a driver type');
  106.     SaveAlias;
  107.   except
  108.     ModalResult := mrNone;
  109.     raise;
  110.   end;
  111. end;
  112.  
  113. procedure TNewAliasForm.SaveAlias;
  114. var
  115.   AliasName: array[0..25] of char;
  116.   DriverName: array[0..25] of char;
  117.   Params: PChar;
  118.   TempPasStr: String;
  119.   TempStr: array[0..255] of char;
  120.   I: Integer;
  121. begin
  122.   StrPCopy(AliasName, Alias.Text);
  123.   StrPCopy(DriverName, Driver.Text);
  124.  
  125.   Params := StrAlloc((DriverParamsGrid.RowCount - 1) * 255);
  126.   try
  127.  
  128.     { Assemble the driver parameters in a single null-terminated string }
  129.     StrPCopy(Params, '');
  130.     with DriverParamsGrid do
  131.     begin
  132.       for I := 0 to RowCount - 2 do
  133.       begin
  134.         TempPasStr := Cells[0, I + 1] + ': ' + Cells[1, I + 1];
  135.         if StrLen(Params) > 0 then TempPasStr := ';' + TempPasStr;
  136.         StrPCopy(TempStr, TempPasStr);
  137.         StrCat(Params, TempStr);
  138.       end;
  139.     end;
  140.  
  141.     case DbiAddAlias(nil, @AliasName, @DriverName, Params, True) of
  142.       DBIERR_INVALIDPARAM:   raise Exception.Create('Invalid Param');
  143.       DBIERR_NAMENOTUNIQUE:  raise Exception.Create('Alias name already exists');
  144.       DBIERR_OBJNOTFOUND:    raise Exception.Create('Invalid driver parameter');
  145.       DBIERR_UNKNOWNDRIVER:  raise Exception.Create('Invalid driver');
  146.     end;
  147.   finally
  148.     StrDispose(Params);
  149.   end;
  150. end;
  151.  
  152. end.
  153.